/**
 * @Project : ProgrammingExercises
 * @Package : ACWing._2LanQiao._2BinarySearch_PrefixSum
 * @File : Q0099.java
 * @Author : WangRuoyu
 * @Date 2023/3/13 23:18
 */

package acwing._2LanQiao._2BinarySearch_PrefixSum;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 * @Project : ProgrammingExercises
 * @Package : ACWing._2LanQiao._2BinarySearch_PrefixSum
 * @File : Q0099.java
 * @Author : WangRuoyu
 * @Date 2023/3/13 23:18
 */

public class Q0099 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        String[] words = line.split(" ");
        int k = Integer.parseInt(words[0]);
        int r = Integer.parseInt(words[1]);
        Scanner scan = new Scanner(System.in);

        int[][] s = new int[5010][5010];

        int xmax = Integer.MIN_VALUE;
        int ymax = Integer.MIN_VALUE;

        for (int i = 0; i < k; i++) {
            line = br.readLine();
            words = line.split(" ");
            int x = Integer.parseInt(words[0]);
            int y = Integer.parseInt(words[1]);
            int value = Integer.parseInt(words[2]);

            s[x + 1][y + 1] += value;
            xmax = Math.max(x, xmax);
            ymax = Math.max(y, ymax);
        }

        int m = xmax + 2;
        int n = ymax + 2;

        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                s[i][j] += +s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1];
            }
        }

        int res = 0;
        if (r >= (m - 1) && r >= (n - 1)) {
            res = s[m - 1][n - 1];
        } else if (r < (m - 1) && r >= (n - 1)) {
            for (int i = r; i < m; i++) {
                res = Math.max(res, s[i][n - 1] + s[i - r][0] - s[i][0] - s[i - r][n - 1]);
            }
        } else if (r >= (m - 1) && r < (n - 1)) {
            for (int j = r; j < n; j++) {
                res = Math.max(res, s[m - 1][j] + s[0][j - r] - s[0][j] - s[m - 1][j - r]);
            }
        } else {
            for (int i = r; i < m; i++) {
                for (int j = r; j < n; j++) {
                    res = Math.max(res, s[i][j] + s[i - r][j - r] - s[i][j - r] - s[i - r][j]);
                }
            }
        }
        System.out.println(res);
    }
}

//5 3
//0 0 1
//0 1 2
//0 2 3
//0 3 4
//0 4 5